/**
 * @author mapeng
 * @version 2017-05-08
 */

(function(doc, win) {

	var pageVariables = {
		"shopId" : "" , 
		"shop" : "" , 
		"handleBack" : "back"
	};

	$(doc).on("pageInit", "#weixinManagePage", function(e, pageId, $page, isBack) {
		initData4Page();
	});
	bindEvent4Page();

	//页面进入
	function handlePageLoad(){
		var params = CommonUtil.takeOutParamObject() || {};
		pageVariables.handleBack = params.handleBack || "back";
		params.handleBack = "";
		CommonUtil.injectParamObject(params);
	}

	//返回
	function doBack(){

		if(pageVariables.handleBack == "back"){
			$.router.back();
		}else if(pageVariables.handleBack == "load"){
			var cache = CommonUtil.takeOutParamObject() || {};
			cache.shopId = pageVariables.shopId;
			cache.handleBack = "load";
			CommonUtil.injectParamObject(cache);
			$.router.load("store_detail.html",true);
		}
	}

	function initData4Page(){

		//缓存变量
		var params = CommonUtil.takeOutParamObject() || {};
		pageVariables.shopId = params.shopId;

		handlePageLoad();

		//查询门店
		CommonUtil.sendJsonpRequest({
		// apiTest.testStoreDetail({
			"url" : App.serverUrl+"/app/shop/getShopDetail.do",
			"params" : {"shopId" : pageVariables.shopId},
			"success" : function(shopResult){
				if(shopResult.success.toString() == "true"){//success

					pageVariables.shop = shopResult.resultObject;

					//查询微信账户
					CommonUtil.sendJsonpRequest({
					// apiTest.testGetShopAccount({
						"url" : App.serverUrl+"/app/wxAccount/getShopAccount.do",
						"params" : {"shopId" : pageVariables.shopId , "merchantId" : pageVariables.shop.merchantId},
						"success" : function(weixinResult){

							//渲染页面
							if(weixinResult.success.toString() == "true"){//success
								CommonUtil.render($("#weixinManagePage .page-body"), "weixinManageTPL",{"data":$.extend({},weixinResult.resultObject,pageVariables.shop),"constants" : Constants},false);
								
								//右侧按钮
								var merchantStatus = pageVariables.shop.merchantStatus;
								var modifyStatus = weixinResult.resultObject.modifyStatus;
								if(merchantStatus == Constants.store.WEIXIN_PAY_STATUS.open){//已开通
									// $("#weixinManagePage .header a[name='operate']").text("变更");
									if(modifyStatus == Constants.weixin.MODIFY_STATUS.auditing){
										// $("#weixinManagePage .header .right").addClass("disabled");//审核中：不允许变更
										// $("#modifyBaseInfo").css('display','none');
										$("#modifyAccountInfo").css('display','none');
										$("#reOpen").css('display','none');
									}else{
										// $("#weixinManagePage .header .right").removeClass("disabled");
										$("#reOpen").css('display','none');
										$("#auditing").css('display','none');
									}
								}else if(merchantStatus == Constants.store.WEIXIN_PAY_STATUS.frozen){//冻结
									// $("#weixinManagePage .header a[name='operate']").text("重新开通");
									// $("#weixinManagePage .header .right").removeClass("disabled");
									$("#modifyBaseInfo").css('display','none');
									$("#modifyAccountInfo").css('display','none');
									$("#auditing").css('display','none');
								}

							}else{//business error
								DialogUtil.dialogWarn(weixinResult.msg);
							}
						},
						"error" : function(){//not 200
							DialogUtil.dialogWarn("请求失败，请稍候重试");
						}
					});

				}else{//business error
					DialogUtil.dialogWarn(shopResult.msg);
				}
			},
			"error" : function(){//not 200
				DialogUtil.dialogWarn("请求失败，请稍候重试");
			}
		});
		
	}

	function bindEvent4Page(){
		
		//返回
		$(document).on("click","#weixinManagePage .header a.back",function(e){
			doBack();
		});
		
		$(document).on("click","#modifyBaseInfo",function(e){
			if(pageVariables.shop.merchantStatus == Constants.store.WEIXIN_PAY_STATUS.open){//变更基础信息
				var cache = CommonUtil.takeOutParamObject() || {};
				cache.shopId = pageVariables.shopId;
				CommonUtil.injectParamObject(cache);
				$.router.load("change_baseInfo.html",true);
			}
		});
		$(document).on("click","#modifyAccountInfo",function(e){
			if(pageVariables.shop.merchantStatus == Constants.store.WEIXIN_PAY_STATUS.open){//变更收款账户
				var cache = CommonUtil.takeOutParamObject() || {};
				cache.shopId = pageVariables.shopId;
				CommonUtil.injectParamObject(cache);
				$.router.load("weixin_change.html",true);
			}
		});
		$(document).on("click","#reOpen",function(e){
			if(pageVariables.shop.merchantStatus == Constants.store.WEIXIN_PAY_STATUS.frozen){//重新开通
				var cache = CommonUtil.takeOutParamObject() || {};
				cache.shopId = pageVariables.shopId;
				cache.type = "2"; //编辑
				CommonUtil.injectParamObject(cache);
				$.router.load("weixin_open_step1.html",true);
			}
		});

		//右侧操作
		// $(document).on("click","#weixinManagePage .header a[name='operate']",function(e){
		// 	if($(this).parents(".right").hasClass("disabled"))	return;

		// 	if(pageVariables.shop.merchantStatus == Constants.store.WEIXIN_PAY_STATUS.open){//变更
		// 		var cache = CommonUtil.takeOutParamObject() || {};
		// 		cache.shopId = pageVariables.shopId;
		// 		CommonUtil.injectParamObject(cache);
		// 		$.router.load("weixin_change.html",true);

		// 	}else if(pageVariables.shop.merchantStatus == Constants.store.WEIXIN_PAY_STATUS.frozen){//重新开通
		// 		var cache = CommonUtil.takeOutParamObject() || {};
		// 		cache.shopId = pageVariables.shopId;
		// 		cache.type = "2"; //编辑
		// 		CommonUtil.injectParamObject(cache);
		// 		$.router.load("weixin_open_step1.html",true);
		// 	}
		// });
	}

	var apiTest = {
		"testGetShopAccount" : function(options){
			options.success({
				"success" : true,
				"resultObject" : {
					"accountCode" : "1234567812345678",
					"bankMainName" : "中国招商银行"
				}
				
			});
		},
		"testStoreDetail" : function(options){
			options.success({
				"success" : true,
				"resultObject" : {
					"shopId" : "1",
					"mainShopName" : "麦当劳",
					"branchShopName" : "上地南路店",
					"categoryLevel1" : "美食",
					"categoryLevel2" : "西餐",
					"categoryLevel3" : "快餐",
					"contactNumber" : "11111111111",
					"address" : "北京市海淀区上地南路88号",
					"isOnline" : "T", //口碑门店状态 T ：上架 F：下架
					"serviceVersionType" : "1",	//订购版本 0：未订购 1：金元宝 2：银元宝 3：快享付
					"proUpgradeStatus" : "1", //订购状态 0：未订购 1：正常 2：已退订 3：已过期
					"expireDate" : "2017-05-08 12:12:12",	
					"alipayPayStatus" : "0", //支付宝开通状态 0：未开通 1：已开通
					"merchantStatus" : "2", //微信开通状态 0：未开通 1：已开通 2：已冻结  3：开通中
					"modifyStatus" : "0", // 0：审核中  1：变更成功 2：变更失败 
					"merchantId" : "111" //微信商户Id
				}
				
			});
		}
	};

})(document, window);
